package com.wyd.jwpkglxt_ssm.controller;

import com.wyd.jwpkglxt_ssm.entity.Department;
import com.wyd.jwpkglxt_ssm.entity.OpeningClassInfo;
import com.wyd.jwpkglxt_ssm.service.OpeningClassInfoService;
import com.wyd.jwpkglxt_ssm.util.ResultCode;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * (OpeningClassInfo)表控制层
 *
 * @author wyd
 * @since 2021-08-12 12:58:10
 */
@RestController
@RequestMapping("openingClassInfo")
public class OpeningClassInfoController {
    /**
     * 服务对象
     */
    @Autowired
    private OpeningClassInfoService openingClassInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public OpeningClassInfo selectOne(Integer id) {
        return this.openingClassInfoService.queryById(id);
    }

    @GetMapping("openingClassInfo/page")
    public ResultUtil getAllOpeningClassInfoByPage(Integer pageNum, Integer pageSize,
                                                   @RequestParam(required = false) String technicalDirection,
                                                   @RequestParam(required = false) String classNature) {
        OpeningClassInfo openingClassInfo = new OpeningClassInfo().setTechnicalDirection(technicalDirection)
                .setClassNature(classNature);
        return ResultUtil.ok().data("pageInfo",this.openingClassInfoService.queryAllByPage(pageNum,pageSize,openingClassInfo));
    }

    @PostMapping("openingClassInfo")
    public ResultUtil addOpeningClassInfo(@RequestBody OpeningClassInfo openingClassInfo){
        if(!Objects.isNull(openingClassInfo)){
            OpeningClassInfo insertOpeningClassInfo = this.openingClassInfoService.insert(openingClassInfo);
            if(!Objects.isNull(insertOpeningClassInfo)){
                return ResultUtil.ok().data("openingClassInfo",insertOpeningClassInfo);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @PutMapping("openingClassInfo")
    public ResultUtil updateOpeningClassInfo(@RequestBody OpeningClassInfo openingClassInfo){
        if(!Objects.isNull(openingClassInfo)){
            OpeningClassInfo updateOpeningClassInfo = this.openingClassInfoService.update(openingClassInfo);
            if(!Objects.isNull(updateOpeningClassInfo)){
                return ResultUtil.ok().data("openingClassInfo",updateOpeningClassInfo);
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }
    //
    @DeleteMapping("openingClassInfo/{openingClassInfoId}")
    public ResultUtil deleteDepartmentById(@PathVariable("openingClassInfoId")String ids){
        boolean b = this.openingClassInfoService.deleteOpeningClassInfoBatch(ids);
        if(b){
            return ResultUtil.ok();
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

}